package net.net23.sysdent.client.ui.widgets.dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class T4LDialog {

	public static void showClosebleDialog(Widget widget) {
		final DialogBox dialogBox = createDialogBox(widget, true);
		dialogBox.setGlassEnabled(true);
		dialogBox.setAnimationEnabled(true);
		dialogBox.center();
		dialogBox.show();
	}

	public static DialogBox getDialogBox(String title, Widget widget, boolean withCloseButton) {
		final DialogBox dialogBox = createDialogBox(widget, withCloseButton);
		dialogBox.setText(title);
		dialogBox.setGlassEnabled(true);
		dialogBox.setAnimationEnabled(true);
		dialogBox.center();
		return dialogBox;
	}

	private static DialogBox createDialogBox(Widget widget,
			boolean withCloseButton) {
		// Create a dialog box and set the caption text
		final DialogBox dialogBox = new DialogBox();

		// Create a table to layout the content
		VerticalPanel dialogContents = new VerticalPanel();
		dialogContents.setSpacing(4);
		dialogBox.setWidget(dialogContents);

		// Add some text to the top of the dialog
		dialogContents.add(widget);
		dialogContents.setCellHorizontalAlignment(widget,
				HasHorizontalAlignment.ALIGN_CENTER);

		if (withCloseButton) {
			// Add a close button at the bottom of the dialog
			Button closeButton = new Button("Cerrar", new ClickHandler() {
				public void onClick(ClickEvent event) {
					dialogBox.hide();
				}
			});
			dialogContents.add(closeButton);
			if (LocaleInfo.getCurrentLocale().isRTL()) {
				dialogContents.setCellHorizontalAlignment(closeButton,
						HasHorizontalAlignment.ALIGN_LEFT);

			} else {
				dialogContents.setCellHorizontalAlignment(closeButton,
						HasHorizontalAlignment.ALIGN_RIGHT);
			}
		}

		// Return the dialog box
		return dialogBox;
	}
}
